package saucaca.fragment;

import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.CustomView.CircularImageView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.SingerObject;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ArtistDetailContent extends Fragment {
	SingerObject singerObject;
	ImageView artistAvatar;
	TextView txtName,txtInfor, txtBirthday, txtSex;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	Context context;
	WebView webView;
	DisplayImageOptions options;
	String singerCode;
	List<SingerObject> listSinger;

	public ArtistDetailContent(Context context, SingerObject singerObject) {
		this.context = context;
		this.singerObject = singerObject;
		this.singerCode = singerObject.getSINGER_ID();
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
				.cacheInMemory().cacheOnDisc().build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View android = inflater.inflate(R.layout.artist_details_content, container, false);
		findviewByID(android);
		ImageLoader.getInstance().displayImage(singerObject.getAVATAR(), artistAvatar, options, null);
		showInfoSinger(singerObject);
		new getArtistDetail().execute("");
		return android;
	}

	void findviewByID(View view) {
		artistAvatar = (ImageView) view.findViewById(R.id.artist_pictureInfo);
		txtName = (TextView) view.findViewById(R.id.artist_txtNameInfo);
		webView = (WebView) view.findViewById(R.id.artist_webInfo);
//		webView.setBackgroundColor(Color.TRANSPARENT);
		 txtInfor = (TextView) view.findViewById(R.id.artist_txtInfo);
		txtBirthday = (TextView) view.findViewById(R.id.artist_txtBirthday);
		txtSex = (TextView) view.findViewById(R.id.artist_txtSex);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
	}

	public void showInfoSinger(SingerObject singerObject) {
		if (singerObject != null) {
			txtName.setText(singerObject.getTXNAME());
			try {
//			String shortdesc = singerObject.getSHORTDESC();
				final String mimeType = "text/html";
				final String encoding = "utf-8";
				String data =Html.fromHtml(singerObject.getSHORTDESC()).toString();
				webView.loadDataWithBaseURL(null,data, mimeType, encoding,null);
			} catch (Exception e) {
			}
			
			txtBirthday.setText(singerObject.getBIRTHDAY());
			txtSex.setText(BaseEngine.getSex(singerObject.getSEX()));

		} else {
			progressBar.setVisibility(View.GONE);
			imgEmpty.setVisibility(View.VISIBLE);
			txtStatus.setText("Không có thông tin!");
		}
	}

	class getArtistDetail extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... param) {
			String str = WServiceLib.artistDetai(singerCode);
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					BaseEngine.showLogformLongString(result);
					listSinger = JsonReader.getArtistDetai(result);
					if (listSinger != null && listSinger.size() > 0)
						showInfoSinger(listSinger.get(0));
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}

	}
}
